"""
交易 - 退款查询相关模型
"""

from typing import Dict, Any, Optional
from jlpay.model.base import OrgBaseReq, BaseResponse


class RefundsQueryRequest(OrgBaseReq):
    """退款查询请求"""
    
    API_URL = "/open/trans/refunds-query"
    
    def __init__(self):
        """初始化退款查询请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
        return self
    
    def set_transaction_id(self, transaction_id: str):
        """设置平台订单号"""
        self.transaction_id = transaction_id
        return self
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no
        })
        return data


class RefundsQueryResponse(BaseResponse):
    """退款查询响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化退款查询响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 1：待确认 2：成功 3：失败
        self.mch_id = None  # 商户号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.chn_transaction_id = None  # 渠道订单号
        self.ori_out_trade_no = None  # 原外部订单号
        self.ori_transaction_id = None  # 原平台订单号
        self.trans_time = None  # 交易时间
        self.success_time = None  # 退款完成时间
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.chn_transaction_id = response_dict.get('chn_transaction_id')
            self.ori_out_trade_no = response_dict.get('ori_out_trade_no')
            self.ori_transaction_id = response_dict.get('ori_transaction_id')
            self.trans_time = response_dict.get('trans_time')
            self.success_time = response_dict.get('success_time')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'chn_transaction_id': self.chn_transaction_id,
            'ori_out_trade_no': self.ori_out_trade_no,
            'ori_transaction_id': self.ori_transaction_id,
            'trans_time': self.trans_time,
            'success_time': self.success_time
        })
        return data 